package fr.insalyon.sims.broadcast.tests;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import fr.insalyon.sims.broadcast.network.DataErrorEvent;
import fr.insalyon.sims.broadcast.network.DataErrorListener;
import fr.insalyon.sims.broadcast.network.DataEvent;
import fr.insalyon.sims.broadcast.network.DataListener;
import fr.insalyon.sims.broadcast.network.MCastStreamTransmitter;
import fr.insalyon.sims.broadcast.network.TCPStreamReceiver;
import fr.insalyon.sims.broadcast.service.BroadcastReceiverService;
import fr.insalyon.sims.broadcast.tools.InetTools;
import fr.insalyon.sims.streamManagerClient.Response;

public class ReceiverServiceTest {
	// ------------------------------------------------------- CONSTANTS -- //
	// ------------------------------------------------------ ATTRIBUTES -- //
	// ---------------------------------------------------- CONSTRUCTORS -- //
	// --------------------------------------------------------- METHODS -- //
	
	public static void main(String[] args) throws IOException {
		int destPort = 22593;
		String multicastIp = "224.0.0.1";
		int multicastPort = 22594;
		
		// Create TCP receiver stream
		TCPStreamReceiver receiver = new TCPStreamReceiver(destPort);
		receiver.addDataListener(new DataListener() {
			public void dataReceived(DataEvent event) {
				String data = new String(event.getData(), event.getOffset(), event.getLength());
				System.out.println("[receiver] data received: " + data);
			}
		});
		receiver.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[receiver] an exception occured!");
				event.getException().printStackTrace();
			}
		});

		// Start the stream
		receiver.connect();
		receiver.start();
		System.out.println("[receiver] started");
		
		// Service parameters
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("destIp", "127.0.0.1");
		params.put("destPort", String.valueOf(destPort));
		params.put("multicastIp", multicastIp);
		params.put("multicastPort", String.valueOf(multicastPort));
		
		// Create and start the receiver service
		BroadcastReceiverService service = new BroadcastReceiverService();
		Response r = service.doStart(params);
		System.out.println("[service] " + r);
		
		// Create multicast transmitter stream
		MCastStreamTransmitter transmitter = new MCastStreamTransmitter(
				InetAddress.getByAddress(InetTools.stringToIp(multicastIp)), multicastPort);
		transmitter.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[transmitter] an exception occured!");
				event.getException().printStackTrace();
			}
		});
		
		// Start the stream
		transmitter.connect();
		transmitter.start();
		System.out.println("[transmitter] started");
		
		// Send an hello world every second
		for (int i = 0; i < 5; i++) {
			String data = "hello world !";
			
			transmitter.send(data.getBytes(), 0, data.length());
			
			System.out.println("[transmitter] hello world sent");

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// Ignored
			}
		}
		
		// Stop streams
		transmitter.stop();
		transmitter.disconnect();
		System.out.println("[transmitter] stopped");
		r = service.doStop();
		System.out.println("[service] " + r);
		receiver.stop();
		receiver.disconnect();
		System.out.println("[receiver] stopped");
	}
	
	// ------------------------------------------------- PRIVATE METHODS -- //
	// --------------------------------------------------- INNER CLASSES -- //
}
